package service;
// 用户服务类

import model.User;
import Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Map<String, Object> register(User user) {
        Map<String, Object> response = new HashMap<>();
        if (userRepository.findByUsername(user.getUsername()) != null) {
            response.put("success", false);
            response.put("message", "该用户名已存在，请重新选择。");
        } else {
            userRepository.save(user);
            response.put("success", true);
            response.put("message", "注册成功！");
        }
        return response;
    }

    public Map<String, Object> login(User user) {
        Map<String, Object> response = new HashMap<>();
        User existingUser = userRepository.findByUsername(user.getUsername());
        if (existingUser != null && existingUser.getPassword().equals(user.getPassword())) {
            response.put("success", true);
            response.put("message", "登录成功！");
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误，请重试。");
        }
        return response;
    }
}